#Configure the HuaweiCloud Provider
terraform {
  required_providers {
    huaweicloud = {
      source = "local-registry/huaweicloud/huaweicloud"
      version = "= 1.34.1"
    }
  }
}

provider "huaweicloud" {
  region     ="cn-north-4"
  access_key = ""
  secret_key = ""
}

resource "huaweicloud_obs_bucket" "bucket" {
  bucket = var.bucket_name
  acl    = "public-read"
  multi_az = true
}
resource "huaweicloud_obs_bucket_policy" "policy" {
  bucket = huaweicloud_obs_bucket.bucket.id
  policy = <<POLICY
  {
        "Statement": [{
                "Action": ["GetObject"],
                "Effect": "Allow",
                "Principal": {
                        "ID": "*"
                },
                "Resource": "${var.bucket_name}/*",
                "Sid": "AddPerm"
        }]
  }
  POLICY
}

resource "huaweicloud_cdn_domain" "domain" {
  name = var.accelerate_domain
  type = var.service_type

sources {
    origin = "${var.bucket_name}.obs.cn-north-4.myhuaweicloud.com"
    origin_type = "obs_bucket"
    active      = 1
  }
}

resource "huaweicloud_dns_zone" "zone" {
  name        = var.publicZone_domain
  ttl         = 3000
  zone_type   = "public"
}

resource "huaweicloud_dns_recordset" "rs_example_com" {
  zone_id     = huaweicloud_dns_zone.zone.id
  name        = var.accelerate_domain
  description = "record set"
  ttl         = 3000
  type        = "CNAME"
  records     = [huaweicloud_cdn_domain.domain.cname]
}
